package com.yc.sole.erp.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.sole.erp.dto.CategoryDto;
import com.yc.sole.erp.entity.Category;
import com.yc.sole.erp.mapper.CategoryMapper;
import com.yc.sole.erp.request.CategoryQueryReq;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2023-12-13
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class CategoryRepository extends ServiceImpl<CategoryMapper, Category> {

    /**
     * 新增
     *
     * @param categoryDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveCategory(CategoryDto categoryDto) {
        Category entity = new Category();
        BeanUtil.copyProperties(categoryDto, entity);
        this.save(entity);
    }

    /**
     * 通过Id修改
     *
     * @param categoryDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateCategoryById(CategoryDto categoryDto) {
        Category entity = new Category();
        BeanUtil.copyProperties(categoryDto, entity);
        this.updateById(entity);
    }

    /**
     * 通过id删除
     *
     * @param ids
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteCategoryByIds(Collection<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 根据名称获取
     *
     * @param names
     * @return RestResult
     */
    public List<CategoryDto> selectCategoryByNames(Set<String> names) {
        if (CollUtil.isEmpty(names)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<Category> wrapper = Wrappers.<Category>lambdaQuery().in(Category::getCategoryName, names);
        return BeanUtil.copyToList(this.list(wrapper), CategoryDto.class);
    }

    /**
     * 根据id获取
     *
     * @param ids
     * @return RestResult
     */
    public List<CategoryDto> selectCategoryByIds(Set<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return BeanUtil.copyToList(this.listByIds(ids), CategoryDto.class);
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return RestResult
     */
    public CategoryDto selectCategoryById(Long id) {
        return BeanUtil.copyProperties(this.getById(id), CategoryDto.class);
    }

    /**
     * 查询分类
     *
     * @return
     */
    public List<CategoryDto> selectCategoryList(CategoryQueryReq categoryQueryReq) {
        LambdaQueryWrapper<Category> queryWrapper = Wrappers.<Category>lambdaQuery()
                .like(CharSequenceUtil.isNotBlank(categoryQueryReq.getCategoryName()), Category::getCategoryName, categoryQueryReq.getCategoryName())
                .orderByAsc(Category::getSort);
        return BeanUtil.copyToList(this.list(queryWrapper), CategoryDto.class);
    }

    /**
     * 根据父级id查询
     *
     * @param parentId
     * @return
     */
    public List<CategoryDto> selectByParentId(Long parentId) {
        LambdaQueryWrapper<Category> wrapper = Wrappers.<Category>lambdaQuery().eq(Category::getParentId, parentId);
        return BeanUtil.copyToList(this.list(wrapper), CategoryDto.class);
    }
}
